<%@page import="java.util.Collection"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueListMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueList"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueDate"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueNumberMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueTextMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueText"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%-- imports --%>
<%@page import="com.dilean.impl.model.PersonImpl"%>
<%@page import="com.dilean.api.model.Person"%>
<%@page import="com.dilean.impl.dao.PersonDAOImpl"%>
<%@page import="com.dilean.api.dao.PersonDAO"%>
<%@page import="com.dilean.spring.AplicationContextHolder"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8">
    <title>My page &middot; Dilean</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- CSS and other resourses -->
    <link href="resources/mytheme/css/bootstrap.min.css" rel="stylesheet">
    <link href="resources/mytheme/css/simple-sidebar.css" rel="stylesheet">
    <link href="resources/mytheme/css/font-awesome.css" rel="stylesheet">
    <link href="resources/mytheme/css/font-awesome.min.css" rel="stylesheet">
    <script src="resources/mytheme/js/jquery-1.11.0.js"></script>
    <script src="resources/mytheme/js/bootstrap.min.js"></script>

    <body>
        <%
                                    //view will be the same, but will not be such connecting after 
                                    // including Spring Security and Spring MVC
                                    String email = "", password = "";
                                    if (request.getParameter("email") != null) {
                                        email = request.getParameter("email");
                                    }
                                    if (request.getParameter("password") != null) {
                                        password = request.getParameter("password");
                                    }
                                    Connection con = null;
                                    PreparedStatement pst = null;
                                    ResultSet re = null;
                                    //Change it for your DataBase
                                    String url = "jdbc:oracle:thin:@localhost:1521:orcl";
                                    String Driver = "oracle.jdbc.driver.OracleDriver";
                                    String user = "NETCRACKER";
                                    String pass = "NETCRACKER";
                                    //
                                    Class.forName(Driver);
                                    con = DriverManager.getConnection(url, user, pass);
                                    String sql = "select per.id, per.name from persons  per, parameters par1,parameters par2 where per.id=par1.pers_id and per.id=par2.PERS_ID and par1.attr_id=13 and par2.attr_id=3 and par1.value = ? and par2.value=?";
                                    try {
                                        pst = con.prepareStatement(sql);
                                        pst.setString(2, email);
                                        String new_pass = "" + password.hashCode();
                                        //encoding
                                        //but without Spring Security it's usefull
                                        //cos in BCrypt hash has some random part
                                        //BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
                                        //String hashedPassword = passwordEncoder.encode(password);
                                        pst.setString(1, password);
                                        re = pst.executeQuery();
                                        if (re.next()) {
                                            int id = Integer.parseInt(re.getString("ID"));

                                            Person me = new PersonImpl();
                                            me = AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).findByIdWithFriends(id);
                                            AttrValueText firstName = me.getAttributeValue(1, AttrValueText.class);
                                            AttrValueText lastName = me.getAttributeValue(2, AttrValueText.class);
                                            AttrValueTextMultiple emails = me.getAttributeValue(3, AttrValueTextMultiple.class);
                                            AttrValueNumberMultiple telephone = me.getAttributeValue(4, AttrValueNumberMultiple.class);
                                            AttrValueDate birthDate = me.getAttributeValue(10, AttrValueDate.class);
                                            AttrValueText status = me.getAttributeValue(5, AttrValueText.class);
                                            AttrValueDate regDate = me.getAttributeValue(11, AttrValueDate.class);
                                            AttrValueList gender = me.getAttributeValue(6, AttrValueList.class);
                                            AttrValueList relStatus = me.getAttributeValue(7, AttrValueList.class);
                                            AttrValueList univercity = me.getAttributeValue(9, AttrValueList.class);
                                            AttrValueListMultiple languages = me.getAttributeValue(8, AttrValueListMultiple.class);

                                            Collection<Person> friendRequests = me.getFriendRequests();
                                            Collection<Person> friends = me.getFriends();
                                            int numberRequest=0;
                                            for(Person fr : friendRequests){
                                                if(friends.contains(fr) == false){
                                                    numberRequest++;
                                                }
                                            }
                                            
                                            String fullname = "User";
                                            if ((firstName != null) && (lastName != null)) {
                                                fullname = firstName.getValue() + " " + lastName.getValue();
                                            } else if ((firstName == null) && (lastName != null)) {
                                                fullname = lastName.getValue();
                                            } else if ((firstName != null) && (lastName == null)) {
                                                fullname = firstName.getValue();
                                            }
                                %>
        <div id="wrapper">
            <!-- Sidebar -->
            <div id="sidebar-wrapper">
                <ul class="sidebar-nav">
                    <li class="sidebar-brand">
                        <a href="http://localhost:8080/Dilean/myPage.jsp">
                            <h2 align="centre" >Dilean</h2>
                        </a>
                    </li>
                    <li>
                        <a href="http://localhost:8080/Dilean/myPage.jsp"><i class="fa fa-home fa-fw-5x"></i>&nbsp; My Page</a>
                    </li>
                    <li>
                        <a href="http://localhost:8080/Dilean/friends.jsp?id=1"><i class="fa fa-users"></i>&nbsp; Friends </a>
                    </li>
                    
                    <li>
                        <a href="http://localhost:8080/Dilean/allUsers.jsp?id=1"><i class="fa fa-database"></i>&nbsp; Users 
                            <% 
                            if(numberRequest!=0){
                                %>
                                <span class="badge badge-info"><%=numberRequest %> </span>
                                <%
                                }
                            %>
                        </a>
                    </li>
                    
                    
                    
                    <li>
                        <a href="http://localhost:8080/Dilean/conversation.jsp"><i class="fa fa-envelope"></i>&nbsp; Messages </a>
                    </li>
                    <li class="divider"></li>
                    <li>
                        <a href="http://localhost:8080/Dilean/editUser.jsp?id=1"><i class="fa fa-cog"></i>&nbsp; Settings</a>
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-book"></i>&nbsp; About</a>
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-sign-out"></i>&nbsp; LogOut</a>
                    </li>

                </ul>
            </div>
            <!-- /#sidebar-wrapper -->

            
            <!-- Page Content -->
            <div id="page-content-wrapper">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-lg-12">

                            <a class="btn btn-large btn-primary" href="#menu-toggle" id="menu-toggle" >
                                <i class="fa fa-user fa-3x"></i> <h4>User Menu</h4></a>
                                
                            <!-- /Main menu -->

                            <!-- user Profile -->
                            <div class="container" >
                                <div class="row">
                                    <div class="col-md-5  toppad  pull-right col-md-offset-3 ">
                                    </div>
                                    <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 col-xs-offset-0 col-sm-offset-0 col-md-offset-3 col-lg-offset-3 toppad" >
                                        <div class="panel panel-info">
                                            <div class="panel-heading">
                                                <h2 class="panel-title" align="center"><%= fullname%></h2>
                                            </div>
                                            <div class="panel-body">
                                                <div class="row">
                                                    <div class="col-md-3 col-lg-3 " align="center"> <img alt="User Pic" src="https://lh5.googleusercontent.com/-b0-k99FZlyE/AAAAAAAAAAI/AAAAAAAAAAA/eu7opA4byxI/photo.jpg?sz=100" class="img-circle"> </div>
                                                    <div class=" col-md-9 col-lg-9 "> 
                                                        <table class="table table-user-information">
                                                            <tbody>
                                                                <%
                                                                    if (status != null) {
                                                                %>
                                                                <tr>
                                                                    <th><h5>Status</h5></th>
                                                            <th><h5><%= status.getValue()%></h5></th>
                                                            </tr>
                                                            <% }
                                                                if (emails != null) {
                                                            %>
                                                            <tr>
                                                                <td><h5>Email </h5></td><td>
                                                                    <%
                                                                        for (int i = 0; i < emails.getValue().size(); i++) {
                                                                            int j = i + 1;
                                                                            String emailStr = emails.getValue().get(i);
                                                                    %>
                                                                    <h5><a href="mailto:<%=emails.getValue().get(i)%>"><%= emails.getValue().get(i)%></a></h5>
                                                                        <%
                                                                            }
                                                                        %>
                                                                </td>
                                                            </tr> 
                                                            <%                                            }
                                                                if (telephone != null) {
                                                            %>
                                                            <tr>
                                                                <td><h5>Telephone number </h5></td><td>
                                                                    <%
                                                                        for (int i = 0; i < emails.getValue().size(); i++) {
                                                                            int j = i + 1;
                                                                    %>
                                                                    <h5> <%= telephone.getValue().get(i)%></h5>
                                                                    <%
                                                                        }
                                                                    %>
                                                                </td>
                                                            </tr> 
                                                            <%                                            }
                                                                if (birthDate != null) {
                                                            %>
                                                            <tr>
                                                                <th><h5>Birth Data</h5></th>
                                                            <th><h5><%= birthDate.getDataStr() %></h5></th>
                                                            </tr>
                                                            <% }
                                                                if (regDate != null) {
                                                            %>
                                                            <tr>
                                                                <th><h5>Registration Data</h5></th>
                                                            <th><h5><%= regDate.getDataStr()  %></h5></th>
                                                            </tr>
                                                            <% }
                                                                if (gender != null) {
                                                            %>
                                                            <tr>
                                                                <th><h5>Gender</h5></th>
                                                            <th><h5><%= gender.getValue().getName()%></h5></th>
                                                            </tr>
                                                            <% }
                                                                if (relStatus != null) {
                                                            %>
                                                            <tr>
                                                                <th><h5>Relationship Status</h5></th>
                                                            <th><h5><%= relStatus.getValue().getName()%></h5></th>
                                                                <% }
                                                                    if (univercity != null) {
                                                                %>
                                                            <tr>
                                                                <th><h5>Univercity</h5></th>
                                                            <th><h5><%= univercity.getValue().getName()%></h5></th>
                                                            </tr>
                                                            <% }
                                                                if (languages != null) {
                                                            %>
                                                            <tr>
                                                                <td><h5>Languages</h5></td><td>
                                                                    <%
                                                                        for (int i = 0; i < languages.getValue().size(); i++) {
                                                                            int j = i + 1;
                                                                    %>
                                                                    <h5><%= languages.getValue().get(i).getName()%></h5>
                                                                    <%
                                                                        }
                                                                    %>
                                                                </td>
                                                                <% }
                                                                %>
                                                            </tr>                                                               
                                                            </tbody>
                                                        </table>

                                                    </div>
                                                </div>
                                            </div>
                                            <div class="panel-footer">
                                                
                                                        
                                                        <a class="btn btn-warning" href="http://localhost:8080/Dilean/editUser.jsp?id=<%= id%>"><i class="fa fa-pencil"></i> Edit My Page</a>
                                                <span class="pull-right">
                                                    <a class="btn btn-danger" href="#"><i class="fa fa-sign-out"></i> LogOut</a>
                                                </span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>             
                            <!-- /user Profile -->
                            <%        } else {
                                        out.println("LOGIN FAILED!!!");
                                    }
                                    con.close();
                                    pst.close();
                                    re.close();
                                } catch (Exception e) {

                                    out.println("ERROR CONNECTING");
                                }
                            %>


                        </div>
                        <script>
                            $("#menu-toggle").click(function(e) {
                                e.preventDefault();
                                $("#wrapper").toggleClass("toggled");
                            });
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>
